<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no" name="viewport" />
    <meta content="yes" name="apple-mobile-web-app-capable">
    <title>canvas</title>
    <style>
        body{
            padding:0;
            margin:0;
        }
        canvas{
            background:#eee;
            padding:0;
        }
        div{
            color: #000;
            background: #ddd;
            font-size: 16px;
        }
    </style>
</head>
<body>
    <canvas id="canvas"></canvas>
    <script src='../js/ball.js'></script>
    <script src="../js/arrow.js"></script>
    <script src='../js/utils.js'></script>
    <script>
        window.onload = function(){
            var canvas = document.getElementById("canvas");
            var context = canvas.getContext('2d');

            canvas.width = document.body.clientWidth;
            canvas.height = document.body.scrollHeight;
            
            // 获取鼠标相对canvas的位置
            var mouse = utils.capture(canvas);

            var ball = new Ball();
                ball.x = canvas.width/4;
                ball.y = canvas.height/4;
            var speed = 2;
            var angle = 30;
            var vr = 2;

            var arrow = new Arrow();
            arrow.x = canvas.width/2;
            arrow.y = canvas.height/2;


            var vx = 1; //定义速度
            var vy = 1; //定义速度
            
            //动画循环中
            (function drawFrame(){
                window.requestAnimationFrame(drawFrame, canvas);
                context.clearRect(0, 0, canvas.width, canvas.height);
                
               // ball.x += vx; // 在每一帧中都让小球的位置加上速度
               //  ball.y += vy // 在每一帧中都让小球的位置加上速度

               // 斜着运动将速度分解到X轴和Y轴，注意这里用的是弧度制，要乘上Math.PI/180
               var vx = Math.cos(angle*Math.PI/180)*speed;
               var vy = Math.sin(angle*Math.PI/180)*speed;

                // 鼠标跟随
               
                ball.x += vx; // 在每一帧中都让小球的位置加上速度
                ball.y += vy // 在每一帧中都让小球的位置加上速度
               // ball.draw(context);

                // 计算角度
                var dx = mouse.x -arrow.x;
                var dy = mouse.y - arrow.y;


                // 获取角度
                angle = Math.atan2(dy,dx);

                // 箭头旋转
                arrow.rotation = angle;  //angle 默认弧度

                // 将速度分解
                var vxx = Math.cos(angle)*speed;
                var vyy = Math.sin(angle)*speed;

                // 分解2
              /*  arrow.rotation = vr*Math.PI/180;
                console.log( arrow.rotation);
                arrow.x += Math.cos(arrow.rotation)*speed;
                arrow.y += Math.sin(arrow.rotatin)*speed;*/

                // 移动
                arrow.x +=vxx;
                arrow.y +=vyy;

                // 绘制
                arrow.draw(context)
            }());
           
        }
    </script>
</body>
</html>
